package com.southminority.ethnic.controller.admin.course;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.controller.admin.vo.AdminPageReqVo;
import com.southminority.ethnic.pojo.CourseEvaluation;
import com.southminority.ethnic.service.course.CourseEvaluationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

/**
 * 课程评价后台管理Controller
 * 
 * @author SouthMinority
 */
@Slf4j
@Tag(name = "课程评价管理后台", description = "管理员课程评价管理接口")
@RestController
@RequestMapping("/admin/course/evaluation")
@CrossOrigin
@Validated
public class CourseEvaluationAdminController {

    @Autowired
    private CourseEvaluationService courseEvaluationService;

    /**
     * 分页查询课程评价列表
     */
    @Operation(summary = "分页查询课程评价列表")
    @PostMapping("/page")
    public R<Page<CourseEvaluation>> getEvaluationPage(@Valid @RequestBody AdminPageReqVo reqVo) {
        try {
            Page<CourseEvaluation> page = new Page<>(reqVo.getPageNum(), reqVo.getPageSize());
            
            // 构建查询条件
            Page<CourseEvaluation> result = courseEvaluationService.getEvaluationPageWithKeyword(page, reqVo.getKeyword());
            
            log.info("管理员分页查询课程评价列表成功，页数: {}, 页面大小: {}, 关键词: {}", 
                    reqVo.getPageNum(), reqVo.getPageSize(), reqVo.getKeyword());
            return R.ok(result);
            
        } catch (Exception e) {
            log.error("管理员分页查询课程评价列表失败", e);
            return R.fail("查询失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取课程评价详情
     */
    @Operation(summary = "根据ID获取课程评价详情")
    @PostMapping("/detail/{id}")
    public R<CourseEvaluation> getEvaluationDetail(@PathVariable Long id) {
        try {
            CourseEvaluation evaluation = courseEvaluationService.getById(id);
            if (evaluation == null) {
                return R.fail("课程评价不存在");
            }
            
            log.info("管理员获取课程评价详情成功，评价ID: {}", id);
            return R.ok(evaluation);
            
        } catch (Exception e) {
            log.error("管理员获取课程评价详情失败，评价ID: {}", id, e);
            return R.fail("获取详情失败：" + e.getMessage());
        }
    }

    /**
     * 删除课程评价
     */
    @Operation(summary = "删除课程评价")
    @PostMapping("/delete/{id}")
    public R<Boolean> deleteEvaluation(@PathVariable Long id) {
        try {
            boolean deleted = courseEvaluationService.removeById(id);
            if (deleted) {
                log.info("管理员删除课程评价成功，评价ID: {}", id);
                return R.ok(true);
            } else {
                return R.fail("删除失败");
            }
            
        } catch (Exception e) {
            log.error("管理员删除课程评价失败，评价ID: {}", id, e);
            return R.fail("删除失败：" + e.getMessage());
        }
    }
} 